<?php
$user = $this->ci->session->userdata('user');

if ($user) {
	$passwd = $this->ci->session->userdata('passwd');
} else {
	$user = $this->ci->input->post('user');
	$passwd = $this->ci->input->post('passwd');
}
?>

<?php
if (empty($user)&&empty($passwd)) {
	//REQUEST LOGIN
	$this->ci->load->view('form_login');
?>

<?php
} else {
	$logok=$this->ci->user_model->get(array('name' => $user,'passwd' => $passwd))->num_rows()>0;
	if (!$logok) {
		$this->ci->load->view('login_error');
?>
<?php
	} else {
		$this->ci->session->set_userdata(array('user' => $user,'passwd' => $passwd));

		if (!empty($_POST['module'])) {
			$module=$_POST['module'];
		} elseif (!empty($_GET['module'])) {
			$module=$_GET['module'];
		} else {
			$module = $this->ci->session->userdata('module');
		}
		$style_menu = array();
		switch ($module) {
		case 'categories': 
			$this->ci->load->model('category_model');

			$submodule = isset($_GET['submodule']) ? $_GET['submodule'] : NULL;
			$action = isset($_GET['action']) ? $_GET['action'] : NULL;
			$data = array();

			switch($action)
			{
			case 'create':
				if(!empty($_GET['name']))
				{
					$res = $this->ci->category_model->add(array('name' => $_GET['name']));

					if($res)
					{
						$data['text_success'] = 'Categor&iacute;a agregada';
					}
					else
					{
						$data['text_error'] = 'Error al tratar de agregar categor&iacute;a';
					}
				}
				break;
			case 'delete':
				if(!empty($_GET['id']))
				{
					$res = $this->ci->category_model->delete(array('id' => $_GET['id']));
					if($res)
					{
						$data['text_success'] = 'Categor&iacute;a eliminada';
					}
					else
					{
						$data['text_error'] = 'Error al tratar de eliminar categor&iacute;a';
					}
				}
				break;
			case 'update':
				if(!empty($_GET['name'])&&!empty($_GET['id']))
				{
					$res = $this->ci->category_model->update(array('id' => $_GET['id'], 'name' => $_GET['name']));
					if($res)
					{
						$data['text_success'] = 'Categor&iacute;a actualizada';
					}
					else
					{
						$data['text_error'] = 'Error al tratar de actualizar categor&iacute;a';
					}
				}
				break;
			}

			switch($submodule)
			{
			case 'new':
				$include_view = $this->ci->load->view('categories_new', array(), TRUE);
				break;
			case 'edit':
				$data['category'] = $this->ci->category_model->get(array('id' => $this->ci->input->get('id')));
				if(empty($data['category']))
				{
					$include_view = $this->ci->load->view('categories_404', array(), TRUE);
				}
				else
				{
					$include_view = $this->ci->load->view('categories_edit', $data, TRUE);
				}
				$include_view = $this->ci->load->view('categories_edit', $data, TRUE);
				break;
			default:
				$data['categories'] = $this->ci->category_model->count_books();
				$include_view = $this->ci->load->view('categories', $data, TRUE);
			}
			$style_menu[1]=" class='active'";
			break;
		case 'books': 
			$submodule = isset($_GET['submodule']) ? $_GET['submodule'] : NULL;
			$action = isset($_GET['action']) ? $_GET['action'] : NULL;
			$this->ci->load->model('book_model');
			$this->ci->load->model('category_model');
			$data = array();

			switch($action)
			{
			case 'create':
				if(!empty($_GET['name'])&&isset($_GET['categories_id']))
				{
					$res = $this->ci->book_model->add($_GET);

					if($res)
					{
						$data['text_success'] = 'Libro agregado';
					}
					else
					{
						$data['text_error'] = 'Error al tratar de agregar libro';
					}
				}
				break;
			case 'delete':
				$res = $this->ci->book_model->delete(array('id' => $_GET['id']));
				if($res)
				{
					$data['text_success'] = 'Libro eliminado';
				}
				else
				{
					$data['text_error'] = 'Error al tratar de eliminar libro';
				}
				break;
			case 'update':
				$res = $this->ci->book_model->update($_GET);
				if($res)
				{
					$data['text_success'] = 'Libro actualizado';
				}
				else
				{
					$data ['text_error'] = 'Error al tratar de actualizar libro';
				}
				break;
			}

			$data['categories'] = $this->ci->category_model->get()->result();
			$data['categories_id'] = $this->ci->input->get('categories_id');


			switch($submodule)
			{
			case 'new':
				$include_view=$this->ci->load->view('books_new', $data, TRUE);break;
			case 'edit':
				$data['row'] = $this->ci->book_model->get(array('id' => $this->ci->input->get('id')));

				if(empty($data['row']))
				{
					$include_view=$this->ci->load->view('books_404', array(), TRUE);
				}
				else
				{
					$include_view=$this->ci->load->view('books_edit', $data, TRUE);
				}
				break;
			default:
				$data['books'] = $this->ci->book_model->get(!empty($data['categories_id']) ? $data : array())->result();
				$include_view=$this->ci->load->view('books', $data, TRUE);
			}
			$style_menu[2]=' class="active"';
			break;
		case 'users':
			$this->ci->load->model('user_model');
			$data = array(
				'users' => $this->ci->user_model->get()->result()
			);
			$include_view = $this->ci->load->view('users', $data, TRUE);
			$style_menu[3]=' class="active"';
			break;
		case 'logout': 
			$this->ci->session->unset_userdata(array('user' => '', 'passwd' => ''));
			redirect('home/index','refresh');
			break;
		default: 
			$this->ci->load->model('book_model');
			$this->ci->load->model('category_model');

			/* codigo de vista*/
			$data = array(
				'qty_books' => $this->ci->book_model->get()->num_rows(),
				'qty_categories' => $this->ci->category_model->get()->num_rows()
			);

			$include_view = $this->ci->load->view('home',$data, TRUE);
			$style_menu[0]=' class="active"';
			break;
		}

		$this->ci->load->view('menu', array('style_menu' => $style_menu));

		/* Medida provisoria para ir moviendo codigo a vistas */
		if(!empty($include_view))
		{
			echo $include_view;
		}
?>
<?php
	}
}
?>
